let api = require('../../../api/index');

Page({

    /**
     * 页面的初始数据
     */
    data: {
        image: '',
        active: 1,
        list: [],
        form: {
            page: 1,
            pagesize: 10,
        },
        pageStatus: 1 // 1下拉加载更多..  5加载中... 10没有更多了... 99暂无数据
    },
    onLoad: function (options) {
        this.reload()
    },
    onShow: function () {

    },
    onReady: function () {

    },
    onHide: function () {

    },
    onUnload: function () {

    },
    onPullDownRefresh: function () {
        console.log('打印日志，要不然会出现一个版本bug')
        
        wx.startPullDownRefresh()

        setTimeout(() => {
            this.getList('reset')

            // 停止下拉刷新
            wx.stopPullDownRefresh()
        }, 0)
    },
    onReachBottom: function () {
        this.getList()
    },
    // onShareAppMessage: function () {

    // },
    reload() {
        console.log('初次加载')
        this.getList('reset')
        this.getImage()
    },
    getImage(){
        let that = this
        api.doctor.getActivityImage().then(res => {
            console.log(res)
            res.data.ResData.map( e => {
                console.log(e)
                if(e.type == 2){
                    that.setData({
                        image: e.image
                    })
                }
            })
        })
    },
    getList(type) {
        if (type === 'reset') {
            this.setData({
                ['form.page']: 1,
                // list: [],
                pageStatus: 1
            })
        }


        if (this.data.pageStatus !== 1) {
            return false
        } else {
            this.setData({
                pageStatus: 5
            })
        }


        api.collage.getCollageGoods({
            page: this.data.form.page,
            pagesize: this.data.form.pagesize,
            type: this.data.active
        }).then(res => {
            let list = res.data.ResData.Items
            let pageSize = res.data.ResData.ItemsPerPage
            let pageStatus;
            list.map((item, index) => {
                // let imgList=[]
                // if (item.GoodsDec.GoodsFileDtos==null) {
                //     imgList = item.GoodsDec
                // }else{
                //     imgList = item.GoodsDec.GoodsFileDto
                // }
                // console.log(imgList);
                let imgList = item.GoodsDec.GoodsFileDtos||item.GoodsDec
                let imgUrl = '';

                imgList.forEach((item2, index2) => {
                    if (index2 == 0) {
                        imgUrl = item2.FilePath
                    } else if (item2.IsCover == true) {
                        imgUrl = item2.FilePath
                    }
                })

                item.imgUrl = imgUrl
            })

            if (this.data.list.pageNum > 1) {
                list = this.data.list.concat(list);
            }

            if (list.length < pageSize) {
                if (list.length <= 0) {
                    pageStatus = 99
                } else {
                    pageStatus = 10
                }
            } else {
                pageStatus = 1
            }

            this.setData({
                list: list,
                pageStatus: pageStatus,
                ['form.page']: this.data.form.page++,
            })
        })
    },
    active_fn(el) {
        this.setData({
            active: el.currentTarget.dataset.id
        })

        this.getList('reset')
    },
    toPagesDetails(e) {
        let info = e.currentTarget.dataset.item;

        wx.navigateTo({
            url: `/pages/product/detail/detail?source=pingtuan&id=${info.Id}`,
        })
    }
})